package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DBHelper;

/**
 * Servlet implementation class RegisteER
 */
@WebServlet("/RegisteER")
public class RegisteER extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisteER() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void handRegisteER(HttpServletRequest request, HttpServletResponse response){
    	try {
    		
    		HttpSession session=request.getSession(false);
    		if(session.getAttribute("user")==null)
        	{
				String employerUsername = request.getParameter("employerUsername");
				String name = request.getParameter("name");
				String password = request.getParameter("psw");
				String address = request.getParameter("address");
				String phoneNumber1 = request.getParameter("phoneNumber1");
				String phoneNumber2 = request.getParameter("phoneNumber2");
				String phoneNumber = phoneNumber1 + " " + phoneNumber2;
				String mail = request.getParameter("mail");
				Connection con = DBHelper.getConnection();
				PreparedStatement sql = DBHelper.getprepareStatement(con,"insert into EMPLOYER values(?,?,?,?,?,?)");
				sql.setString(1, employerUsername);
				sql.setString(2, name);
				sql.setString(3, address);
				sql.setString(4, phoneNumber);
				sql.setString(5, mail);
				sql.setString(6, null);
				sql.executeUpdate();
				sql.close();
				PreparedStatement sql1= DBHelper.getprepareStatement(con,"insert into JOBLOGIN values(?,?,?)");
				sql1.setString(1, employerUsername);
				sql1.setString(2, password);
				sql1.setString(3, "employer");
				sql1.executeUpdate();
				sql1.close();
				con.close();
				session.setAttribute("user", employerUsername); 	
    			
    			request.getRequestDispatcher("employer.jsp").forward(request,response);
        	}
        	else
        		response.sendRedirect("login.jsp");
    					
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		handRegisteER(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		handRegisteER(request, response);
	}

}
